<template>
  <div>
    <el-card class="search-card">
      <basic-table-form
        ref="formData"
        :formColumns="formItemList"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
        :colNumOfRow="4"
      >
      </basic-table-form>
    </el-card>
    <el-card class="table-card">
      <basic-table
        :showSelectCol="false"
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :tableHeight="500"
        showActionCol
        :actionColWidth="150"
        :actionRender="renderTableBtnAction"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
        <div slot="content_context">
          <el-button
            v-if="showBtn('导出') && supplierExportConfig.supervision"
            :loading="exportLoading"
            size="mini"
            type="primary"
            @click="handleExport"
            >导出</el-button
          >
        </div>
      </basic-table>
    </el-card>
    <BackUp :backUpVisible.sync="backUpVisible" :selectId="selectId" @getTableData="getTableData" />
  </div>
</template>
<script>
import BasicTableForm from '@/components/supplier/BasicForm';
import BasicTable from '@/components/supplier/BasicTable';
import BackUp from './components/BackUp';
import { FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';
import BasicPage from '@/mixins/supplier/basicPage';
import { queryToBeCheckListService, exportToBeCheckService } from '@api/supplier/supervision/toBeCheck';
import { createExcelByExport, setAuthBtnId } from '@/utils/bizUtils';
import { getTableAction } from '@/pages/supplier/utils/btnsUtils';

export default {
  mixins: [ BasicPage ], // 分页数据及方法 表单基础方法
  components: {
    BasicTableForm,
    BasicTable,
    BackUp
  },
  data () {
    return {
      backUpVisible: false,
      supplierData: {}, // 转派
      batchPushDownLoading: false,
      formItemList: FORM_ITEM_LIST,
      tableInfo: {
        list: [
          // { id: 1, checkStatusDesc: '待处理', checkStatus: '1', }
        ],
        selection: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      formData: {
        'belongDepartment': null,
        'checkDimension': null,
        'checkType': null,
        'checkUser': '',
        'deptCategory': null,
        'exceptionType': null,
        'isCheck': null,
        'managementAreaId': null,
        'supplierCode': '',
        'supplierName': '',
        'supplierType': null
      },
      selectId: 0
    };
  },
  created () {
    this.getTableData();
  },
  methods: {
    renderTableBtnAction (h, { row }) {
      let actionList = [];
      // 1 待核查 2已核查
      if (row.checkStatus == 1) {
        actionList = [
          { label: '下推价格核查', method: this.handlePushCheck, hasAuth: true },
          { label: '无问题', method: this.handleEdit, hasAuth: true },
        ];
      } else if (row.processBillId) {
        actionList = [
          { label: '查看', method: this.handleView },
        ];
      }
      return getTableAction({ h, row, actionList, showBtn: this.showBtn });
    },
    handleView (row) {
      this.$router.push({ name: 'viewRegularPriceSupervision', query: { processId: row.processBillId } });
    },
    // 下推价格核查
    handlePushCheck (row) {
      const { supplierId, supplierCode, id, processBillId } = row
      this.$router.push({ name: 'addRegularPriceSupervision', query: { supplierId, supplierCode, supplierSupervisionDetailId: id, processBillId } })
    },
    // 无问题
    handleEdit (row) {
      this.selectId = row.id;
      this.backUpVisible = true;
    },
    // 获取表格数据
    async getTableData () {
      let data = {
        ...this.formData,
        ...this.pagination
      };
      setAuthBtnId()
      const res = await queryToBeCheckListService(data, state => { this.tableInfo.loading = state })
      if (res) {
        const { records, total } = res.result;
        this.tableInfo.list = records;
        this.pagination.total = total;
      }
    },
    // 导出
    async handleExport () {
      const res = await exportToBeCheckService(this.formData, state => { this.exportLoading = state })
      if (res) {
        createExcelByExport({ excelTitle: '待核查列表', res })
      }
    },
  }
};
</script>
